import os
from building import *

cwd = GetCurrentDir()

Import('asenv')
ASROOT = asenv['ASROOT']

MODULES = ['ECUM','SCHM','MCU',
           'DET','SHELL',
           'USB','USB_SERIAL',
           'CLIB_ASHEAP','CLIB_MBOX','CLIB_MISCLIB',
           'CLIB_STRTOK_R','CLIB_STDIO_PRINTF',
           'ASKAR',
           ]


ARCH='at91sam3s'
arch='cortex-m'

MODULES += ['CLIB_MBOX']

if(asenv['MODULES'] is None):
    asenv['MODULES'] = MODULES
else:
    MODULES = asenv['MODULES']

asenv['ARCH'] = ARCH
asenv['arch'] = arch

objs = []

asenv.Append(CPPPATH=['%s/../board.posix/common'%(cwd)])

if('MCU' in MODULES):
    objs += Glob('../board.posix/common/Mcu*.c')

if('DEMO_LED_APP' in MODULES):
    objs += Glob('leds/*.xml')
    objs += Glob('leds/*.c')

if('USB_SERIAL' in MODULES):
    objs += Glob('usb/*.xml')
    objs += Glob('usb/*.c')

asenv.Append(CPPDEFINES=['CHIP_AT91SAM3S','sam3s4'])

if(asenv['RELEASE']=='asboot'):
    asenv['LINKSCRIPT'] = '%s/script/linker-boot.lds'%(cwd)
else:
    asenv['LINKSCRIPT'] = '%s/script/linker-app.lds'%(cwd)

asenv.Append(CPPDEFINES=['ENABLE_SHELL_ECHO_BACK','OS_STK_SIZE_SCALER=1/2'])

if('run' in COMMAND_LINE_TARGETS):
    SelectCompilerArmNoneEabi()
    gdb = asenv['CC'].split(' ')[0][:-3]+'gdb'
    cmd = 'cd %s/release/ascore && %s -x %s/script/rom.gdbinit'%(ASROOT,gdb, cwd)
    RunCommand(cmd)
    exit()
Return('objs')
